package com.zhkj.txg.module.order.adapter

import android.view.View
import android.widget.EditText
import android.widget.ImageView
import androidx.recyclerview.widget.LinearLayoutManager
import androidx.recyclerview.widget.RecyclerView
import com.chad.library.adapter.base.BaseQuickAdapter
import com.chad.library.adapter.base.viewholder.BaseViewHolder
import com.zhkj.lib.utils.AccountManager
import com.zhkj.lib.utils.SpanUtil
import com.zhkj.txg.R
import com.zhkj.txg.module.order.entity.CartEntity
import com.zhkj.txg.module.order.entity.OrderGoodsEntity
import com.zhkj.txg.utils.ImageUtils
import com.zhkj.txg.utils.TextWatcherWrapper

class OrderCartAdapter(
    private val onDescChange: (uiPosition: Int, content: String?) -> Unit
) :
    BaseQuickAdapter<CartEntity, OrderShopViewHolder>(R.layout.item_order_shop) {

    init {
        addChildClickViewIds(R.id.viewSelectShopCoupon)
    }

    override fun convert(helper: OrderShopViewHolder, item: CartEntity) {
        val imageView = helper.setText(R.id.tvOrderShopName, item.shopName)
            .setText(
                R.id.tvShopTotalFreight,
                if (item.isFreeFreight) context.getString(R.string.freight_free)
                else SpanUtil.setMoneySymbol(item.freight.toString())
            )
            .setText(
                R.id.tvShopCouponName,
                if (item.selectCoupon == null) "" else item.selectCoupon!!.name
            ).setText(R.id.tvOrderGoodsDesc, item.orderDesc)
            .setText(R.id.tvOrderShopGoodsCount, "共${item.shopTotalNum}件，小计")
            .setText(R.id.tvOrderShopAmount, SpanUtil.setMoneySymbol(item.useCouponTotalPrice.toString()))
            .setGone(
                R.id.viewCut,
                !AccountManager.isMember() || item.cutPrice.isNullOrEmpty() || item.cutPrice.toDouble() <= 0
            )
            .setText(R.id.tvOrderSave, item.cutPrice)
            .getView<ImageView>(R.id.ivOrderShop)
        ImageUtils.loadCircleImage(context, imageView, item.shopImg)
        helper.goodsAdapter.replaceData(item.goodsList)
    }

    override fun onItemViewHolderCreated(viewHolder: OrderShopViewHolder, viewType: Int) {
        with(viewHolder.getView<RecyclerView>(R.id.rvOrderGoods)) {
            layoutManager = LinearLayoutManager(context)
            adapter = viewHolder.goodsAdapter
        }
        viewHolder.getView<EditText>(R.id.tvOrderGoodsDesc).addTextChangedListener(TextWatcherWrapper {
            onDescChange(viewHolder.layoutPosition, it?.toString())
        })
    }
}

class OrderShopViewHolder(view: View) : BaseViewHolder(view) {
    val goodsAdapter = OrderGoodsAdapter()
}

class OrderGoodsAdapter :
    BaseQuickAdapter<OrderGoodsEntity, BaseViewHolder>(R.layout.item_order_goods) {
    override fun convert(helper: BaseViewHolder, item: OrderGoodsEntity) {
        val imageView = helper.setText(R.id.tvOrderGoodsName, item.goodsName)
            .setText(
                R.id.tvOrderGoodsPrice,
                SpanUtil.setMoneySymbol(
                    item.goodsPrice.toString(),
                    AccountManager.isMember() && !item.shareEarn.isNullOrEmpty() && item.shareEarn.toDouble() > 0
                )
            ).setText(R.id.tvOrderGoodsNumber, "x${item.goodsNumber}")
            .getView<ImageView>(R.id.ivOrderGoods)
        ImageUtils.loadRoundCornerImage(context, imageView, item.image)
    }
}